home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / andere sprachen / perl5 / perl5.002 / utils / perldoc.pl < prev    next >
Encoding:
Perl Script  |  1996-01-23  |  9.8 KB  |  386 lines

  1. #!/usr/local/bin/perl
  2.  
  3. use Config;
  4. use File::Basename qw(&basename &dirname);
  5.  
  6. # List explicitly here the variables you want Configure to
  7. # generate.  Metaconfig only looks for shell variables, so you
  8. # have to mention them as if they were shell variables, not
  9. # %Config entries.  Thus you write
  10. #  $startperl
  11. # to ensure Configure will look for $Config{startperl}.
  12.  
  13. # This forces PL files to create target in same directory as PL file.
  14. # This is so that make depend always knows where to find PL derivatives.
  15. chdir(dirname($0));
  16. ($file = basename($0)) =~ s/\.PL$//;
  17. $file =~ s/\.pl$//
  18.     if ($Config{'osname'} eq 'VMS' or
  19.         $Config{'osname'} eq 'OS2');  # "case-forgiving"
  20.  
  21. open OUT,">$file" or die "Can't create $file: $!";
  22.  
  23. print "Extracting $file (with variable substitutions)\n";
  24.  
  25. # In this section, perl variables will be expanded during extraction.
  26. # You can use $Config{...} to use Configure variables.
  27.  
  28. print OUT <<"!GROK!THIS!";
  29. $Config{'startperl'}
  30.     eval 'exec perl -S \$0 "\$@"'
  31.     if 0;
  32. !GROK!THIS!
  33.  
  34. # In the following, perl variables are not expanded during extraction.
  35.  
  36. print OUT <<'!NO!SUBS!';
  37.     eval 'exec perl -S $0 "$@"'
  38.     if 0;
  39.  
  40. #
  41. # Perldoc revision #1 -- look up a piece of documentation in .pod format that
  42. # is embedded in the perl installation tree.
  43. #
  44. # This is not to be confused with Tom Christianson's perlman, which is a
  45. # man replacement, written in perl. This perldoc is strictly for reading
  46. # the perl manuals, though it too is written in perl.
  47. #
  48. # Version 1.11: Tue Dec 26 09:54:33 EST 1995
  49. #       Kenneth Albanowski <kjahds@kjahds.com>
  50. #   -added Charles Bailey's further VMS patches, and -u switch
  51. #   -added -t switch, with pod2text support
  52. # Version 1.10: Thu Nov  9 07:23:47 EST 1995
  53. #        Kenneth Albanowski <kjahds@kjahds.com>
  54. #    -added VMS support
  55. #    -added better error recognition (on no found pages, just exit. On
  56. #     missing nroff/pod2man, just display raw pod.)
  57. #    -added recursive/case-insensitive matching (thanks, Andreas). This
  58. #     slows things down a bit, unfortunately. Give a precise name, and
  59. #     it'll run faster.
  60. #
  61. # Version 1.01:    Tue May 30 14:47:34 EDT 1995
  62. #        Andy Dougherty  <doughera@lafcol.lafayette.edu>
  63. #   -added pod documentation.
  64. #   -added PATH searching.
  65. #   -added searching pod/ subdirectory (mainly to pick up perlfunc.pod
  66. #    and friends.
  67. #
  68. #
  69. # TODO:
  70. #
  71. #    Cache directories read during sloppy match
  72. #
  73.  
  74. =head1 NAME
  75.  
  76. perldoc - Look up Perl documentation in pod format.
  77.  
  78. =head1 SYNOPSIS
  79.  
  80. B<perldoc> [B<-h>] [B<-v>] [B<-t>] [B<-u>] PageName|ModuleName|ProgramName
  81.  
  82. =head1 DESCRIPTION
  83.  
  84. I<perldoc> looks up a piece of documentation in .pod format that is
  85. embedded in the perl installation tree or in a perl script, and displays
  86. it via pod2man | nroff -man | $PAGER.  This is primarily used for the
  87. documentation for the perl library modules. 
  88.  
  89. Your system may also have man pages installed for those modules, in
  90. which case you can probably just use the man(1) command.
  91.  
  92. =head1 OPTIONS
  93.  
  94. =over 5
  95.  
  96. =item B<-h> help
  97.  
  98. Prints out a brief help message.
  99.  
  100. =item B<-v> verbose
  101.  
  102. Describes search for the item in detail.
  103.  
  104. =item B<-t> text output
  105.  
  106. Display docs using plain text converter, instead of nroff. This may be faster,
  107. but it won't look as nice.
  108.  
  109. =item B<-u> unformatted
  110.  
  111. Find docs only; skip reformatting by pod2*
  112.  
  113. =item B<PageName|ModuleName|ProgramName>
  114.  
  115. The item you want to look up.  Nested modules (such as C<File::Basename>)
  116. are specified either as C<File::Basename> or C<File/Basename>.  You may also
  117. give a descriptive name of a page, such as C<perlfunc>. You make also give a
  118. partial or wrong-case name, such as "basename" for "File::Basename", but
  119. this will be slower, if there is more then one page with the same partial
  120. name, you will only get the first one.
  121.  
  122. =back
  123.  
  124. =head1 ENVIRONMENT
  125.  
  126. Any switches in the C<PERLDOC> environment variable will be used before the 
  127. command line arguments.  C<perldoc> also searches directories
  128. specified by the C<PERL5LIB> (or C<PERLLIB> if C<PERL5LIB> is not
  129. defined) and C<PATH> environment variables.
  130. (The latter is so that embedded pods for executables, such as
  131. C<perldoc> itself, are available.)
  132.  
  133. =head1 AUTHOR
  134.  
  135. Kenneth Albanowski <kjahds@kjahds.com>
  136.  
  137. Minor updates by Andy Dougherty <doughera@lafcol.lafayette.edu>
  138.  
  139. =head1 SEE ALSO
  140.  
  141. =head1 DIAGNOSTICS
  142.  
  143. =cut
  144.  
  145. if(@ARGV<1) {
  146.     die <<EOF;
  147. Usage: $0 [-h] [-v] [-t] [-u] PageName|ModuleName|ProgramName
  148.  
  149. We suggest you use "perldoc perldoc" to get aquainted 
  150. with the system.
  151. EOF
  152. }
  153.  
  154. use Getopt::Std;
  155. use Config;
  156. $Is_VMS = $Config{'osname'} eq 'VMS';
  157.  
  158. sub usage{
  159.         warn "@_\n" if @_;
  160.     die <<EOF;
  161. perldoc [-h] [-v] [-u] PageName|ModuleName|ProgramName...
  162.     -h   Display this help message.
  163.     -t   Display pod using pod2text instead of pod2man and nroff.
  164.     -u     Display unformatted pod text
  165.     -v     Verbosely describe what's going on.
  166. PageName|ModuleName...
  167.          is the name of a piece of documentation that you want to look at. You 
  168.          may either give a descriptive name of the page (as in the case of
  169.          `perlfunc') the name of a module, either like `Term::Info', 
  170.          `Term/Info', the partial name of a module, like `info', or 
  171.          `makemaker', or the name of a program, like `perldoc'.
  172.          
  173. Any switches in the PERLDOC environment variable will be used before the 
  174. command line arguments.
  175.  
  176. EOF
  177. }
  178.  
  179. use Text::ParseWords;
  180.  
  181.  
  182. unshift(@ARGV,shellwords($ENV{"PERLDOC"}));
  183.  
  184. getopts("htuv") || usage;
  185.  
  186. usage if $opt_h || $opt_h; # avoid -w warning
  187.  
  188. eval "use Pod::Text" if $opt_t;
  189.  
  190. @pages = @ARGV;
  191.  
  192. # VMS only -- use this hack until support for searchlist
  193. # logical names is better integrated into the Perl core
  194. sub translate_searchlist_logical {
  195.     my($lnm) = @_;
  196.     my($trans,@trans);
  197.     return unless $ENV{$lnm};
  198.     $trans = `show logical $lnm`;
  199.     $trans =~ s/\n1(.|\n)*//;  # clip off iterative translations
  200.     @trans = split(/[\"=\s\n]+/,$trans); # break into words
  201.     splice(@trans,0,2); # pop off initial blank and orig name
  202.     @trans = grep(!/^\(/,@trans); # filter out table names
  203.     wantarray ? @trans : $trans[0];
  204. }
  205.  
  206. sub containspod {
  207.     my($file) = @_;
  208.     local($_);
  209.     open(TEST,"<$file");
  210.     while(<TEST>) {
  211.         if(/^=head/) {
  212.             close(TEST);
  213.             return 1;
  214.         }
  215.     }
  216.     close(TEST);
  217.     return 0;
  218. }
  219.  
  220.  sub minus_f_nocase {
  221.      my($file) = @_;
  222.      local *DIR;
  223.      local($")="/";
  224.      my(@p,$p,$cip);
  225.      foreach $p (split(/\//, $file)){
  226.     if ($Is_VMS and not scalar @p) {
  227.         # VMS filesystems don't begin at '/'
  228.         push(@p,$p);
  229.         next;
  230.     }
  231.      if (-d ("@p/$p")){
  232.          push @p, $p;
  233.      } elsif (-f ("@p/$p")) {
  234.          return "@p/$p";
  235.      } else {
  236.          my $found=0;
  237.          my $lcp = lc $p;
  238.          opendir DIR, "@p";
  239.          while ($cip=readdir(DIR)) {
  240.         $cip =~ s/\.dir$// if $Is_VMS;
  241.          if (lc $cip eq $lcp){
  242.              $found++;
  243.              last;
  244.          }
  245.          }
  246.          closedir DIR;
  247.          return "" unless $found;
  248.          push @p, $cip;
  249.          return "@p" if -f "@p";
  250.      }
  251.      }
  252.      return; # is not a file
  253.  }
  254.  
  255.   sub searchfor {
  256.       my($recurse,$s,@dirs) = @_;
  257.       $s =~ s!::!/!g;
  258.       $s = VMS::Filespec::unixify($s) if $Is_VMS;
  259.       printf STDERR "looking for $s in @dirs\n" if $opt_v;
  260.      my $ret;
  261.      my $i;
  262.      my $dir;
  263.       for ($i=0;$i<@dirs;$i++) {
  264.           $dir = $dirs[$i];
  265.           ($dir = VMS::Filespec::unixpath($dir)) =~ s!/$!! if $Is_VMS;
  266.          if ((    $ret = minus_f_nocase "$dir/$s.pod")
  267.          or ( $ret = minus_f_nocase "$dir/$s.pm"  and containspod($ret))
  268.          or ( $ret = minus_f_nocase "$dir/$s"     and containspod($ret))
  269.          or ( $Is_VMS and 
  270.               $ret = minus_f_nocase "$dir/$s.com" and containspod($ret))
  271.          or ( $ret = minus_f_nocase "$dir/pod/$s.pod")
  272.          or ( $ret = minus_f_nocase "$dir/pod/$s" and containspod($ret)))
  273.          { return $ret; }
  274.          
  275.          if($recurse) {
  276.             opendir(D,$dir);
  277.             my(@newdirs) = grep(-d,map("$dir/$_",grep(!/^\.\.?$/,readdir(D))));
  278.             closedir(D);
  279.             @newdirs = map((s/.dir$//,$_)[1],@newdirs) if $Is_VMS;
  280.             last unless @newdirs;
  281.             print STDERR "Also looking in @newdirs\n" if $opt_v;
  282.             push(@dirs,@newdirs);
  283.          }
  284.      }
  285.       return ();
  286.   }
  287.  
  288.  
  289. foreach (@pages) {
  290.     print STDERR "Searching for $_\n" if $opt_v;
  291.     # We must look both in @INC for library modules and in PATH
  292.     # for executables, like h2xs or perldoc itself.
  293.     @searchdirs = @INC;
  294.     if ($Is_VMS) {
  295.         push(@searchdirs, translate_searchlist_logical('DCL$PATH'));
  296.     } else {
  297.         push(@searchdirs, grep(-d, split(':', $ENV{'PATH'})));
  298.     }
  299.     @files= searchfor(0,$_,@searchdirs);
  300.     if( @files ) {
  301.         print STDERR "Found as @files\n" if $opt_v;
  302.     } else {
  303.         # no match, try recursive search
  304.         
  305.         @searchdirs = grep(!/^\.$/,@INC);
  306.         
  307.         
  308.         @files= searchfor(1,$_,@searchdirs);
  309.         if( @files ) {
  310.             print STDERR "Loosely found as @files\n" if $opt_v;
  311.         } else {
  312.             print STDERR "No documentation found for '$_'\n";
  313.         }
  314.     }
  315.     push(@found,@files);
  316. }
  317.  
  318. if(!@found) {
  319.     exit ($Is_VMS ? 98962 : 1);
  320. }
  321.  
  322. if( ! -t STDOUT ) { $opt_f = 1 }
  323.  
  324. unless($Is_VMS) {
  325.     $tmp = "/tmp/perldoc1.$$";
  326.     $goodresult = 0;
  327.     @pagers = qw( more less pg view cat );
  328.     unshift(@pagers,$ENV{PAGER}) if $ENV{PAGER};
  329. } else {
  330.     $tmp = 'Sys$Scratch:perldoc.tmp1_'.$$;
  331.     @pagers = qw( most more less type/page );
  332.     unshift(@pagers,$ENV{PERLDOC_PAGER}) if $ENV{PERLDOC_PAGER};
  333.     $goodresult = 1;
  334. }
  335.  
  336. foreach (@found) {
  337.     
  338.     if($opt_t) {
  339.         open(TMP,">>$tmp");
  340.         Pod::Text::pod2text($_,*TMP);
  341.         close(TMP);
  342.     } elsif(not $opt_u) {
  343.         open(TMP,">>$tmp");
  344.         $rslt = `pod2man $_ | nroff -man`;
  345.         if ($Is_VMS) { $err = !($? % 2) || $rslt =~ /IVVERB/; }
  346.         else      { $err = $?; }
  347.         print TMP $rslt unless $err;
  348.         close TMP;
  349.     }
  350.                                                     
  351.     if( $opt_u or $err or -z $tmp) {
  352.         open(OUT,">>$tmp");
  353.         open(IN,"<$_");
  354.         $cut = 1;
  355.         while (<IN>) {
  356.             $cut = $1 eq 'cut' if /^=(\w+)/;
  357.             next if $cut;
  358.             print OUT;
  359.         }
  360.         close(IN);
  361.         close(OUT);
  362.     }
  363. }
  364.  
  365. if( $opt_f ) {
  366.     open(TMP,"<$tmp");
  367.     print while <TMP>;
  368.     close(TMP);
  369. } else {
  370.     foreach $pager (@pagers) {
  371.         $sts = system("$pager $tmp");
  372.         last if $Is_VMS && ($sts & 1);
  373.         last unless $sts;
  374.     }
  375. }
  376.  
  377. 1 while unlink($tmp); #Possibly pointless VMSism
  378.  
  379. exit 0;
  380. !NO!SUBS!
  381.  
  382. close OUT or die "Can't close $file: $!";
  383. chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
  384. exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';
  385.